﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTestFrameForNFC
{
    [InstrumentInterfaceTypeAnno(InstrumentInterfaceType.GPIB)]
    /**
     * 需要按实际情况定制方法
     */
    class WT1800 : BaseInstrument
    {
        public void InitForAM00000(int VoltageRange = 300, int CurrentRange = 1000, int RefreshRate = 200, int LineFilter = -1, int FreqFilter = -1)
        {
            Write("*RST");
            Write(":DISPLAY:NUMERIC:FRAME ON");

            Write(":DISPLAY:NUMERIC:NORMAL:FORMAT VAL16");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM1 URMS,1");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM2 IRMS,1");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM3 P,1");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM4 LAMBDA,1");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM5 F1,1");


            Write(":DISPLAY:NUMERIC:NORMAL:ITEM6 UDC,2");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM7 IDC,2");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM8 P,2");


            Write(":DISPLAY:NUMERIC:NORMAL:ITEM9 URMS,4");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM10 IRMS,4");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM11 P,4");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM12 LAMBDA,4");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM13 F2,4");


            Write(":DISPLAY:NUMERIC:NORMAL:ITEM14 UDC,5");

            Write(":DISPLAY:NUMERIC:NORMAL:ITEM15 IDC,5");

            Write("DISPLAY:NUMERIC:NORMAL:ITEM16 P,5");




            Write(":MEASURE:FUNCTION1:STATE ON");

            Write(":MEASURE:FUNCTION1:EXPRESSION 'P(E2)/P(E1)*100'");

            Write(":MEASURE:FUNCTION1:NAME 'F1'");

            Write(":MEASURE:FUNCTION1:UNIT '%'");

            Write(":MEASURE:FUNCTION2:STATE ON");

            Write(":MEASURE:FUNCTION2:EXPRESSION 'P(E5)/P(E4)*100'");

            Write(":MEASURE:FUNCTION2:NAME 'F2'");

            Write(":MEASURE:FUNCTION2:UNIT '%'");





            Write(":NUMERIC:NORMAL:NUMBER 16");

            Write(":NUMERIC:NORMAL:ITEM1 URMS,1");

            Write(":NUMERIC:NORMAL:ITEM2 IRMS,1");

            Write(":NUMERIC:NORMAL:ITEM3 P,1");

            Write(":NUMERIC:NORMAL:ITEM4 LAMBDA,1");

            Write(":NUMERIC:NORMAL:ITEM5 F1,1");

            Write(":NUMERIC:NORMAL:ITEM6 UDC,2");

            Write(":NUMERIC:NORMAL:ITEM7 IDC,2");

            Write(":NUMERIC:NORMAL:ITEM8 P,2");

            Write(":NUMERIC:NORMAL:ITEM9 URMS,4");

            Write(":NUMERIC:NORMAL:ITEM10 IRMS,4");

            Write(":NUMERIC:NORMAL:ITEM11 P,4");

            Write(":NUMERIC:NORMAL:ITEM12 LAMBDA,4");

            Write(":NUMERIC:NORMAL:ITEM13 F2,4");
            Write(":NUMERIC:NORMAL:ITEM14 UDC,5");

            Write(":NUMERIC:NORMAL:ITEM15 IDC,5");

            Write(":NUMERIC:NORMAL:ITEM16 P,5");
            //设置量程
            for (int i = 1; i <= 6; i++)
            {
                Write(string.Format(":INPUT:VOLTAGE:RANGE:ELEMENT{0} {1}V", i, VoltageRange));
                Write(string.Format(":INPUT:CURRENT:RANGE:ELEMENT{0} {1}mA", i, CurrentRange));
            }
            //设置刷新率
            Write(string.Format("RATE {0}.0E-03", RefreshRate));
            //设置滤波
            if (LineFilter == -1)
            {
                for (int i = 1; i <= 6; i++)
                {
                    Write(string.Format("INPUT:FILTER:LINE:ELEMENT{0} OFF", i));
                }
            }
            else
            {
                for (int i = 1; i <= 6; i++)
                {
                    Write(string.Format("INPUT:FILTER:LINE:ELEMENT{0} {1}kHZ", i, LineFilter));
                }
            }
            if (FreqFilter == -1)
            {
                for (int i = 1; i <= 6; i++)
                {
                    Write(string.Format("INPUT:FILTER:FREQuency:ELEMENT1 OFF", i));
                }
            }
            else
            {
                for (int i = 1; i <= 6; i++)
                {
                    Write(string.Format("INPUT:FILTER:FREQuency:ELEMENT{0} {1}kHZ", i, FreqFilter));
                }
            }

        }

        public double[] Read(int count)
        {
            string ResultStr = Query(":Numeric:Normal:Value?");
            string[] vs = ResultStr.Split(',');
            string[] vs_temp;
            if (vs.Length != count)
            {
                vs_temp = new string[count];
                Array.Copy(vs, vs_temp, 0);
            }

            double[] Result = new double[count];
            for (int i = 0; i < count; i++)
            {
                if (double.TryParse(vs[i], out double value))
                {
                    Result[i] = value;
                }
                else
                {
                    Result[i] = -1;
                }
            }
            return Result;
        }
    }
}
